# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class SaleAdvancePaymentInvInheritGym(models.TransientModel):
    _inherit = 'sale.advance.payment.inv'

    # 錯誤偵測，總公司不能開票，銷售單不能修改數量。
    @api.multi
    def create_invoices(self):
        if self.env.user.company_id.name == u'總公司':
            raise ValidationError(u'總公司不能開票!!請切換至銷售訂單的所屬公司!')
        gym_order = self.env['sale.order'].browse(self._context.get('active_ids', []))
        for line in gym_order:
            for row in line.order_line:
                for row_line in row.gym_class_ids:
                    if row.product_id == row_line.class_product_id and row.product_uom_qty != row_line.class_amount:
                        raise ValidationError(u'銷售訂單銷售數量與課堂數量不同!!如有修改訂購數量，請取消本單後重新建立銷售訂單!')

        return super(SaleAdvancePaymentInvInheritGym, self).create_invoices()